package com.xxx.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.reggie.common.R;
import com.xxx.reggie.entity.Employee;
import com.xxx.reggie.entity.Page;
import com.xxx.reggie.exception.BusinessException;
import com.xxx.reggie.mapper.EmployeeMapper;
import com.xxx.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    /**
     * 登入用户名
     *
     * @param employee
     * @return R
     */

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;


    @Override
    public R<Employee> login(Employee employee) {

        //1.根据页面提交的用户名username查询数据库中员工数据信息
        Employee login = employeeMapper.login(employee);

        if (login == null){
            return R.error("用户名或密码错误");
        }
        //密码比对,使用密文进行比对
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        if (!login.getPassword().equals(password)){
            return R.error("用户名或者密码错误");
        }

        //查看员工状态,如果已为禁用状态,则员工返回禁用结果
        if (login.getStatus()==0){
           return R.error("已被禁用请联系管理员");
        }

        return R.success(login);
    }

    /**
     * 添加用户
     *
     * @param employee 员工数据
     */
    @Override
    public void save(Employee employee) {
        //校验用户名是否已被注册
        //根据用户名查找用户
        Employee dbEmployee = employeeMapper.login(employee);

        //判断用户是否存在
        if (dbEmployee!=null){
            throw new BusinessException("用户名已存在");
        }

        //设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //设置默认状态
        employee.setStatus(1);

        //设置默认时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        //默认时间
        employee.setCreateTime(now);
        //更新时间
        employee.setUpdateTime(now);

        //调用业务插入数据库
        employeeMapper.save(employee);
    }

    /**
     * 作用:员工列表分页
     *
     * @param page
     * @param pageSize
     * @param name
     * @return Page 返回封装的数据
     */
    @Override
    public Page<Employee> findByName(Integer page, Integer pageSize, String name) {

        //通过PageHelper设置查询多少页,每页多少条
        PageHelper.startPage(page,pageSize);

        //执行数据库列表查询操作
        //PageHelper操作这条SQL语句变成两条分页的SQL语句执行
        List<Employee> EmployeeList = employeeMapper.findByName(name);

        //封装得到PageInfo(类似于以前的PageBean)
        PageInfo<Employee> pageInfo = new PageInfo<>(EmployeeList);
        Page<Employee> pageObj = new Page<>(pageInfo.getList(),
                                            pageInfo.getTotal(),
                                            pageInfo.getPageSize(),
                                            pageInfo.getPageNum());

        return pageObj;
    }


    /**
     * 修改用户的方法
     *
     * @param employee
     */
    @Override
    public void update(Employee employee) {

        //完善员工更改时间信息
        employee.setUpdateTime(LocalDateTime.now());

        //调用业务获取数据
        employeeMapper.update(employee);
    }

    /**
     * 根据id查询员工对象
     *
     * @param id
     * @return Employee
     */
    @Override
    public Employee findById(long id) {
        Employee byId = employeeMapper.findById(id);
        return byId;
    }
}
